package zsc.liangyi.blog.dao;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;
import zsc.liangyi.blog.model.domain.Article;

import java.util.List;


@Component
@Mapper
public interface ArticleDao {
    //根据列名查询信息
    @Select("SELECT * FROM article WHERE ${column}=#{value}")
    public List<Article> selectArticleWithColumn(String column,Object value);

    // 根据id查询文章信息
    @Select("SELECT\n" +
            "\tarticle.*,\n" +
            "\tcategory.`category_name` category_name\n" +
            "FROM\n" +
            "\tarticle \n" +
            "\tJOIN category  ON category.id =article .category_id AND article.id=#{id}")
    public Article selectArticleWithId(Integer id);

    // 根据用户id查询文章信息
    @Select("SELECT * FROM article WHERE user_id=#{user_id} ORDER BY created DESC")
    public List<Article> selectArticleWithPageByUserId(Integer user_id);

    //查询用户收藏的文章
    @Select("SELECT * FROM article a RIGHT JOIN collent t ON  a.id=t.article_id AND t.user_id=#{id}")
    public List<Article> selectArticleWithCollectByUserId(Integer id);

    // 发表文章，同时使用@Options注解获取自动生成的主键id
    @Insert("INSERT INTO article (title,created,modified,tags," +
            " allow_comment, thumbnail, content,user_id,category_id)" +
            " VALUES (#{title},#{created}, #{modified}, #{tags}," +
            " #{allowComment}, #{thumbnail}, #{content},#{userId},#{categoryId})")
    @Options(useGeneratedKeys=true, keyProperty="id", keyColumn="id")
    public Integer publishArticle(Article article);

    // 文章发分页查询,默认发表时间排序
    @Select("SELECT * FROM article ORDER BY created DESC")
    public List<Article> selectArticleWithPage();

    //文章查找，通过title或者contant
    @Select("SELECT * FROM article WHERE title LIKE CONCAT('%',#{keyword},'%') OR content LIKE CONCAT('%',#{keyword},'%')")
    public List<Article> selectArticleWithKeyword(String keyword);

    // 通过id删除文章
    @Delete("DELETE FROM article WHERE id=#{id}")
    public void deleteArticleWithId(Integer id);

    // 站点服务统计，统计文章数量
    @Select("SELECT COUNT(1) FROM article")
    public Integer countArticle();

    // 通过id更新文章,使用xml文件映射
    public Integer updateArticleWithId(Article article);
}
